<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size: 16px;">
      <el-breadcrumb-item :to="{ path: '/sys-serve' }">
        <i class="el-icon-s-promotion"></i> 后台管理
      </el-breadcrumb-item>
      <el-breadcrumb-item>添加服务类别</el-breadcrumb-item>
    </el-breadcrumb>

    <el-divider></el-divider>

    <el-form :model="serveForm" :rules="rules" ref="ruleForm" label-width="120px" class="demo-ruleForm">

      <el-form-item label="服务类名" prop="title">
          <el-select v-model="serveForm.title" placeholder="请选择" style="width: 400px" >
            <el-option
                v-for="item in serverIdList"
                :key="item.serveName"
                :label="item.serveName"
                :value="item.serveName">
            </el-option>
          </el-select>
      </el-form-item>
      <el-form-item label="服务类名称" prop="servicename">
        <el-input v-model="serveForm.servicename" style="width: 400px"></el-input>
      </el-form-item>
      <el-form-item label="服务价格" prop="price">
        <el-input v-model="serveForm.price" style="width: 400px"></el-input>
      </el-form-item>
      <el-form-item label="服务时长" prop="serve_time">
        <el-input v-model="serveForm.serve_time" style="width: 400px"></el-input>
      </el-form-item>
      <el-form-item label="服务所属编号" prop="serveId">
        <el-input v-model="serveForm.serveId" style="width: 400px"></el-input>
      </el-form-item>
<!--      <el-form-item label="服务图片" prop="url">-->
<!--        <el-input v-model="serveForm.url" style="width: 400px"></el-input>-->
<!--      </el-form-item>-->
      <el-form-item label="是否启用" prop="enable">
        <el-switch
            v-model="serveForm.enable"
            :active-value="1"
            :inactive-value="0"
            active-color="#13ce66"
            inactive-color="#ccc">
        </el-switch>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">添加</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  data() {
    return {
      serverIdList:[],
      roleList: [],
      serveForm: {
        serveId: '',
        title:'',
        serve_time:'',
        servicename: '',
        enable: '',
        price:'',
      },
      rules: {
        price: [
          {required: true, message: '请输入服务类名称', trigger: 'blur'},
          {min: 1, max: 10, message: '长度在 1 到 10 个字符', trigger: 'blur'}
        ],
        serve_time: [
          {required: true, message: '请输入服务类名称', trigger: 'blur'},
          {min: 1, max: 10, message: '长度在 1 到 10 个字符', trigger: 'blur'}
        ],
        servicename: [
          {required: true, message: '请输入服务类名称', trigger: 'blur'},
          {min: 2, max: 15, message: '长度在 2 到 15 个字符', trigger: 'blur'}
        ],
        serveId: [
          {required: true, message: '请输入服务类所属ID', trigger: 'blur'},
          {min: 1, max: 10, message: '长度在 1 到 10 个字符', trigger: 'blur'}
            ],
        enable: [
          {required: true, message: '', trigger: 'change'}
        ],
        title: [
          {required: true, message: '请输入服务类名', trigger: 'blur'},
          {min: 2, max: 10, message: '长度在 2 到 10 个字符', trigger: 'blur'}
        ],
      }
    };
  },
  methods: {
    loadServerId(){
      console.log("开始执行loadServerId```")
      let url="http://localhost:9081/serve"
      console.log("url="+url)
      this.axios
          .create({"headers":{'Authorization':localStorage.getItem("jwt")}})
          .get(url).then((response)=>{
        console.log("服务器返回的数据,放入serverIdList:")
        console.log(response.data)
        let responseBody = response.data;
        this.serverIdList=responseBody.data
      })
    },
    loadserveList() {
      console.log('loadserveList ...');
      let url = "http://localhost:9081/serve";
      console.log(url);
      this.axios
          .create({"headers":{'Authorization':localStorage.getItem("jwt")}})
          .get(url).then((respose) => {
        let responseBody = respose.data;
        this.tableData = responseBody.data;
      })
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let url = 'http://localhost:9081/services/add-new';
          console.log('url = ' + url);
          let formData = this.qs.stringify(this.serveForm);
          console.log('formData：' + formData);
          this.axios
              .create({"headers":{'Authorization':localStorage.getItem("jwt")}})
              .post(url, formData).then((response)=> {
            let responseBody = response.data;
            if (responseBody.state == 20000) {
              this.$message({
                message: '添加服务类别成功！',
                type: 'success'
              });
              this.resetForm(formName);
            } else {
              this.$message.error(responseBody.message);
            }
          });
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  },
  mounted() {
    this.loadserveList();
    this.loadServerId();
  }
}
</script>

<style scoped>

</style>